/**
 * \* Created with IntelliJ IDEA.
 * \* User: 冯若航
 * \* Date: 2021/5/17
 * \* Time: 18:55
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class 最长回文子串 {
    public String longestPalindrome1(String s){
        if(s.length()==0){
            return "";
        }

        int strLen = s.length();
        int left = 0;
        int right = 0;
        int len = 1;
        int maxStart = 0;
        int maxLen = 0;

        for(int i=0;i<strLen;i++){
            left=i-1;
            right=i+1;
            while (left>=0&&s.charAt(left) == s.charAt(i)){
                len++;
                left--;
            }
            while (right < strLen && s.charAt(right) == s.charAt(i)) {
                len++;
                right++;
            }
            while (left >= 0 && right < strLen && s.charAt(right) == s.charAt(left)) {
                len = len + 2;
                left--;
                right++;
            }
            if (len > maxLen) {
                maxLen = len;
                maxStart = left;
            }
            len = 1;
        }
        return s.substring(maxStart + 1, maxStart + maxLen + 1);
    }
}